﻿
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using Yoozoo.Gameplay.City;

namespace Yoozoo.Gameplay.CityEditor
{
    public class DecorationEditor
    {
        [MenuItem("Assets/内城/装饰物")]
        public static void CreateDecorationMeshData()
        {
            var gameObjects = Selection.gameObjects;
            for (int i = 0; i < gameObjects.Length; i++)
            {
                var gameObject = gameObjects[i];
                var obj = GameObject.Instantiate(gameObject);
                //检查里面有没有不支持instancing的材质
                var isValid = true;
                var meshRenderers = obj.GetComponentsInChildren<MeshRenderer>();
                for (int j = 0; j < meshRenderers.Length; j++)
                {
                    var meshRenderer = meshRenderers[j];
                    var materials = meshRenderer.sharedMaterials;
                    /*for (int k = 0; k < materials.Length; k++)
                    {
                        if (!materials[k].enableInstancing || DecorationMeshData.IsInBlackList(materials[k].shader.name))
                        {
                            isValid = false;
                            break;
                        }
                    }*/

                    if (!isValid)
                    {
                        break;
                    }
                }

                if (isValid)
                {
                    var decorationMeshData = obj.GetOrAddComponent<DecorationMeshData>();
                    decorationMeshData.AutoCollect();
                    PrefabUtility.SaveAsPrefabAsset(obj, AssetDatabase.GetAssetPath(gameObjects[i]));
                }
                GameObject.DestroyImmediate(obj); 
            }
            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();
        }
    }
}
